﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace JESAI.Common.Util
{
    /// <summary>
    /// 文件操作类
    /// </summary>

    public class FileUtility
    {
        public FileUtility() { }


        /// <summary>
        /// 根据相对路径获取物理路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string MapPath(string path)
        {          
            string rootPath = AppDomain.CurrentDomain.BaseDirectory;

            if (path.StartsWith("~/", StringComparison.CurrentCultureIgnoreCase))
            {
                path = path.Remove(0, 2);
            }
            if (path.StartsWith("/", StringComparison.CurrentCultureIgnoreCase) || path.StartsWith("\\", StringComparison.CurrentCultureIgnoreCase))
            {
                path = path.Remove(0);
            }
            return Path.Combine(rootPath, path);
        }

        public static void SaveFile(MemoryStream stream, string filePath)
        {
            if (stream == null) throw new Exception("流对象为空");
        }

        #region 文件相关

        /// <summary>
        /// 复制文件，不复制子目录文件
        /// </summary>
        /// <param name="sourceDir"></param>
        /// <param name="targetDir"></param>
        /// <param name="overWrite"></param>
        public static void CopyFiles(string sourceDir, string targetDir, bool overWrite)
        {
            CopyFiles(sourceDir, targetDir, overWrite);
        }


        /// <summary>
        /// 复制文件
        /// </summary>
        /// <param name="sourceDir">源目录</param>
        /// <param name="targetDir">目标目录</param>
        /// <param name="overWrite">是否覆盖</param>
        /// <param name="copySubDir">是否复制子目录</param>
        public static void CopyFiles(string sourceDir, string targetDir, bool overWrite, bool copySubDir)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(sourceDir);
            if (dirInfo.Exists)
            {
                foreach (FileInfo fileInfo in dirInfo.GetFiles())
                {
                    string targetFileName = Path.Combine(targetDir, fileInfo.Name);
                    File.Copy(fileInfo.FullName, targetFileName, overWrite);
                }
            }
            if (copySubDir)
            {
                foreach (DirectoryInfo subDirInfo in dirInfo.GetDirectories())
                {
                    string subTargetDir = Path.Combine(targetDir, subDirInfo.Name);
                    CreateDirectory(subTargetDir);
                    CopyFiles(subDirInfo.FullName, subTargetDir, overWrite, true);
                }
            }
        }

        /// <summary>
        /// 移动文件，不包括子目录
        /// </summary>
        /// <param name="sourceDir"></param>
        /// <param name="targetDir"></param>
        public static void MoveFiles(string sourceDir, string targetDir)
        {
            MoveFiles(sourceDir, targetDir, false);
        }

        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="sourceDir"></param>
        /// <param name="targetDir"></param>
        /// <param name="moveSubDir"></param>
        public static void MoveFiles(string sourceDir, string targetDir, bool moveSubDir)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(sourceDir);
            if (dirInfo.Exists)
            {
                foreach (FileInfo fileInfo in dirInfo.GetFiles())
                {
                    string targetFileName = Path.Combine(targetDir, fileInfo.Name);
                    File.SetAttributes(fileInfo.FullName, FileAttributes.Normal);
                    File.Delete(targetFileName);
                    File.Move(fileInfo.FullName, targetFileName);
                }
            }
            if (moveSubDir)
            {
                foreach (DirectoryInfo subDirInfo in dirInfo.GetDirectories())
                {
                    string subTargetDir = Path.Combine(targetDir, subDirInfo.Name);
                    CreateDirectory(subTargetDir);
                    MoveFiles(subDirInfo.FullName, subTargetDir, true);
                    subDirInfo.Delete();
                }
            }
        }

        /// <summary>
        /// 删除指定文件夹文件，不删除子目录文件
        /// </summary>
        /// <param name="targetDir"></param>
        public static void DeleteFiles(string targetDir)
        {
            DeleteFiles(targetDir, false);
        }

        /// <summary>
        /// 删除制定文件夹文件
        /// </summary>
        /// <param name="targetDir"></param>
        /// <param name="delSubDir">是否删除</param>
        public static void DeleteFiles(string targetDir, bool delSubDir)
        {
            foreach (string filePath in Directory.GetFiles(targetDir))
            {
                File.SetAttributes(filePath, FileAttributes.Normal);
                File.Delete(filePath);
            }

            if (delSubDir)
            {
                foreach (string subFilePath in Directory.GetDirectories(targetDir))
                {
                    DeleteFiles(subFilePath, true);
                    Directory.Delete(subFilePath);
                }
            }
        }
        #endregion

        #region 目录相关

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="targetDir"></param>
        public static void CreateDirectory(string targetDir)
        {
            if (Directory.Exists(targetDir) == false)
            {
                Directory.CreateDirectory(targetDir);
            }
        }

        /// <summary>
        /// 删除制定目录
        /// </summary>
        /// <param name="dirPath"></param>
        public static void DeleteDirectory(string targetDir)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(targetDir);
            if (directoryInfo.Exists)
            {
                DeleteFiles(directoryInfo.FullName, true);
                directoryInfo.Delete(true);
            }
        }

        /// <summary>
        /// 删除指定目录的所有子目录，但保留目标目录
        /// </summary>
        /// <param name="targetDir"></param>
        public static void DeleteSubDirectory(string targetDir)
        {
            foreach (string subDir in Directory.GetDirectories(targetDir))
            {
                DeleteDirectory(subDir);
            }
        }

        #endregion
    }
}
